<?php
namespace jy_merch;
defined('BY_JYA') or exit('error');
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_merch/model')->方法名();
*/
class model extends \plugin{
	/*
		核实分销商
	*/
	public function checkmerch($merchid=0,$uid=0){
			$uid=$uid?:$_SESSION['uid'];
			if($merchid){
				$merch=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
				if(empty($merch)){
					exi('没有找到该小店！','error','goback');
				}
				if($merch['status']==0){
					$_SESSION['merchid']='';
					exi('申请正在审核请耐心等待','error','jy_merch/register/');
				}elseif($merch['status']==2){
					$_SESSION['merchid']='';
					exi('审核失败，请修改完善！','error','jy_merch/register/');
				}else{
					$merchid = $merch['id'];
				}
			}elseif($_SESSION['lmerchid']){
				$merch=pdo_get('jy_merch_user',array('id'=>$_SESSION['lmerchid'],'uniacid'=>$_SESSION['uniacid']));
				if($merch['status']==0){
					$_SESSION['merchid']='';
					$_SESSION['lmerchid']='';
					exi('申请正在审核请耐心等待,请重新登录','error','jy_merch/manage.login/');
				}elseif($merch['status']==2){
					$_SESSION['merchid']='';
					$_SESSION['lmerchid']='';
					exi('审核失败，请重新登录！','error','jy_merch/manage.login/');
				}else{
					$merchid = $merch['id'];
				}
			}elseif($uid){
				$merch=pdo_get('jy_merch_user',array('mid'=>$uid,'uniacid'=>$_SESSION['uniacid']));
				if(empty($merch)){
					$worker=pdo_get('jy_merch_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
					if(!$worker['id']){
						exi('您还未注册店铺','error','jy_merch/register/');
					}
					$merch=pdo_get('jy_merch_user',array('id'=>$worker['merchid'],'uniacid'=>$_SESSION['uniacid']));
					if(!$merch['id']){
						exi('您还未注册店铺','error','jy_merch/register/');
					}
				}elseif($merch['status']==0){
					$_SESSION['merchid']='';
					exi('申请正在审核请耐心等待','error','jy_merch/register/');
				}elseif($merch['status']==2){
					$_SESSION['merchid']='';
					exi('审核失败，请修改完善！','error','jy_merch/register/');
				}else{
					$merchid = $merch['id'];
				}
			}
			return $merch;
	}
	/*
		创建门店
	*/
	public function checkadd_merch($mid=0,$status=0,$merchid=0){
		$mid=$mid?:$_SESSION['uid'];
		if($merchid){
			$merch=pdo_get('jy_merch_user',array('id'=>$merchid));
		}
		if($mid!=$merch['mid']){
			$merch=pdo_get('jy_merch_user',array('mid'=>$mid));
		}
		$merchid=$merch['id'];
		if(!$status){
			$status=$this->setting['isshenhe']?0:1;
		}
		if(empty($merchid)){
			$core_members=pdo_get('core_members',array('id'=>$mid),array('nickname','avatar','mobile','realname'));
			$data =array(
				'uname'=>'area_fee'.$mid,
				'mobile'=>$core_members['mobile'],
				'realname'=>$core_members['realname'],
				'merchname'=>$core_members['nickname'],
				'logo'=>$core_members['avatar'],
				'status'=>$status,
				'applytime'=>time(),
				'jointime'=>time(),
			);
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$mid;
			if($this->setting['demo_groupid'] && intval($this->setting['demo_days'])){
				$data['groupid']=$this->setting['demo_groupid'];
				$data['viptime']=TIMESTAMP+intval($this->setting['demo_days'])*24*3600;
			}else{
				$data['groupid']=pdo_getcolumn('jy_merch_group',array('isdefault'=>1,'uniacid'=>$_SESSION['uniacid']),'id');
				$data['viptime']=TIMESTAMP+365*24*3600;
			}
			pdo_insert('jy_merch_user',$data);
			$merchid=pdo_insertid();
		}elseif(!$merch['status'] && $merch['status']!=$status){
			pdo_update('jy_merch_user',array('status'=>$status),array('mid'=>$mid));
		}
		return $merchid;
	}
	public function upinfo_merch($merchid){
		$merch=pdo_get('jy_merch_user',array('id'=>$merchid));
		if(!$merch['merchname']){
			$core_members=pdo_get('core_members',array('id'=>$merch['mid']),array('nickname','avatar','mobile','realname'));
			$data =array(
				'mobile'=>$core_members['mobile'],
				'realname'=>$core_members['realname'],
				'merchname'=>$core_members['nickname'],
				'logo'=>$core_members['avatar'],
			);
			$this->pdo_update('user',$data,array('id'=>$merchid));
		}
		return true;
	}
	public function checkviptime($merch=array()){
		if(!$merch['groupid']){
			$tips='你的店铺还未开通会员，开通会员助力门店营销！';
		}else{
			if($merch['viptime'] && $merch['viptime']<=TIMESTAMP){
				$data=array();
				$data['groupid']=pdo_getcolumn('jy_merch_group',array('isdefault'=>1,'uniacid'=>$_SESSION['uniacid']),'id');
				$data['viptime']=TIMESTAMP+365*24*3600;
				$tips='你的店铺会员已过期，续费会员助力门店营销！';
				$this->pdo_update('user',$data,array('id'=>$merch['id'],'uniacid'=>$_SESSION['uniacid']));
			}elseif($merch['viptime'] && $merch['viptime']<=TIMESTAMP+7*24*3600){
				$lasttime=$merch['viptime']-TIMESTAMP;
				$lastday=intval($lasttime/(24*3600));
				$tips='你的店铺会员【'.$lastday.'】天后将过期，续费会员助力门店营销！';
			}
		}
		return $tips;
	}
	/*
		创建商品
	*/
	public function checkadd_goods($params){
		$mid=$params['mid']?:$_SESSION['uid'];
		$merchid=$this->checkadd_merch($mid);
		$goods=pdo_get('jy_weishop_goods',array('id'=>$params['goodsid']));
		$isgoods=pdo_get('jy_weishop_goods',array('merchid'=>$merchid,'api_id'=>$goods['id'],'api_pluginname'=>'jy_area_fee'));
		if(!$isgoods['id']){
			$goods['total']=$params['total'];
			$goods['api_id']=$goods['id'];
			$goods['api_pluginname']='jy_area_fee';
			$goods['merchid']=$merchid;
			unset($goods['id']);
			pdo_insert('jy_weishop_goods',$goods);
		}else{
			pdo_update('jy_weishop_goods',array('total +='=>$params['total']),array('id'=>$isgoods['id'],'merchid'=>$merchid));
		}
		return true;
	}
	/*
		核实商品数量
	*/
	public function checkgoodsnum($merchid=0,$isexi=false){
		$merch=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		if(!$merch['groupid']){
			return true;
		}
		$group_goodsnum=pdo_getcolumn('jy_merch_group',array('id'=>$merch['groupid']),'goodsnum');
		if($group_goodsnum){
			$isgoodsnum=pdo_count('jy_weishop_goods',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'id');
			if($isgoodsnum>=$group_goodsnum){
				if($isexi){
					exi('发布商品数量上限，不能再添加！','error','/manage.index/');
				}else{
					return false;
				}
			}
		}
		return true;
	}
	/*
		缴纳押金
	*/
	public function checkauth($merchid=0,$isres=false){
		if($this->setting['is_groupkaidian']){
			$groupid=pdo_getcolumn('jy_merch_user',array('id'=>$merchid),'groupid');
			$kaidian=pdo_getcolumn('jy_merch_group',array('id'=>$groupid),'kaidian');
			if(!$kaidian){
				if($isres){
					return 0;
				}else{
					exi('指定商户分组才能开店，请升级商户等级！','error','jy_merch/manage.open/');
				}
			}
		}
		if($this->setting['yajin_open'] && $this->setting['yajin']>0){
			$is_yajinfee=pdo_getcolumn('jy_merch_user',array('id'=>$merchid),'is_yajinfee');
			if(!$is_yajinfee || $is_yajinfee<=0){
				if($isres){
					return 3;
				}else{
					exi('还未缴纳押金不能开小店！','error','jy_merch/manage.setyajin/');
				}
			}
		}
		return 1;
	}
	public function get($params,&$total=null,$lat_lng=array()){
		if(!$params['orderby']){
			$params['orderby']=array('id desc');
		}
		//$list=$this->pdo_getlist('user',$params,$total);
		$list=pdo_getall('jy_merch_user',$params['params'],$params['fields']);
		foreach($list as $k=>&$v){
			$v['logo']=$v['logo']?tomedia($v['logo']):'';
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			if($v['lat_lng']['lng'] && $v['lat_lng']['lat']){
				$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
				$v['juli']=intval($v['juli']/1000);
			}else{
				if($lat_lng){
					unset($list[$k]);
				}
			}
		}
		paixu($list,'juli','asc');
		return $list;
	}
	public function getmerch($parameter,$limit=5,$lat_lng=array(),$page=1,$limitjuli=0){
		$limitjuli=intval($limitjuli);
		if($lat_lng['lat'] || $lat_lng['lng']){
			$lat_lng=array('lat'=>$lat_lng['lat'],'lng'=>$lat_lng['lng']);
			$this->session('lat_lng',$lat_lng);
		}
		$lat_lng=$this->session('lat_lng');
		$total='';
		$parameter['orderby']=$parameter['orderby']?:'id desc';
		$isonlinetime=$parameter['params']['isonlinetime'];
		unset($parameter['params']['isonlinetime']);
		$list=pdo_getslice('jy_merch_user',$parameter['params'],$parameter['limit'],$total,$parameter['fields'],$parameter['key'],$parameter['orderby']);
		$start_goodsnum=$parameter['start_goodsnum']?:$this->setting['start_goodsnum'];
		$goodsparams=array('status'=>1,'nochecked'=>0,'uniacid'=>$_SESSION['uniacid']);
		if($parameter['start_fuwunum']){
			$start_goodsnum=$parameter['start_fuwunum'];
			$goodsparams['type']=4;
		}else{
			$goodsparams['type <>']=4;
		}
		foreach($list as $k=>&$v){
			if($start_goodsnum>0){
				$goodsparams['merchid']=$v['id'];
				$v['goodsnum']=pdo_count('jy_weishop_goods',$goodsparams,'id');
				if($v['goodsnum']<$start_goodsnum){
					unset($list[$k]);
					continue;
				}
			}
			$v['pcd']=json_decode($v['pcd'],true);
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			$v['lat_lng']['lng']=$v['lat_lng']['lng']?:'114.424632';
			$v['lat_lng']['lat']=$v['lat_lng']['lat']?:'23.126942';
			$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
			$v['juli']=intval($v['juli']/1000)?:2;
			if($limitjuli && $v['juli']>$limitjuli){
				unset($list[$k]);
				continue;
			}
			if($isonlinetime){
				$v['onlinetime']=json_decode($v['onlinetime'],true);
				$v['onlinetime']['start']=$v['onlinetime']['start']?:'7:00';
				$v['onlinetime']['end']=$v['onlinetime']['end']?:'22:00';	
				$star=strtotime($days.' '.$v['onlinetime']['start']);
				$end=strtotime($days.' '.$v['onlinetime']['end']);
				if($end<$star){
					$end=$end+24*3600;
				}
				if(($star>TIMESTAMP || TIMESTAMP>$end)){
					$v['offline']=1;
				}
			}
			if($v['offline']==1 && ($this->setting['no_offline'] || $parameter['no_offline'])){
				unset($list[$k]);
				continue;
			}
			$v['paixu']=$v['juli'];
			if($v['isrecommand']){
				$v['paixu']=$v['juli']>30?$v['juli']-30:0;
			}
			if($parameter['orderbykey']=='star' && $v['star']>0){
				$v['paixu']=$v['juli']>30?$v['juli']-30:0;
				$paixu=5/$v['star'];
				$v['paixu']=$v['paixu']+$paixu;
			}
            if($parameter['orderbykey']=='salenum' || $parameter['orderbykey']=='istuijie'){
                $v['paixu']=0-$v['salenum'];
            }
            if($parameter['orderbykey']=='out_price'){
                $v['paixu']=0-$v['out_price'];
            }
            if($parameter['orderbykey']=='out_pay'){
                $v['paixu']=0-$v['out_pay'];
            }
			if($v['offline']){
				$v['paixu'] +=500;
			}
			$merchsetting=json_decode($v['setting'],true);
			$v['pai_feerate']=$merchsetting['paifeenew']?:$parameter['pai_feerate'];
		}
		if($parameter['order']=='fanfee'){
			paixu($list,'pai_feerate','desc');
		}else{
			paixu($list,'paixu');
		}
		if($limit){
			$list=array_slice($list,0,intval($limit));
		}else{
			$page=$page?:1;
			$start=($page-1)*10;
			$list=array_slice($list,$start,10);
		}
		return $list;
	}
	public function out_times($merch_id,$post=array()) {
		$store=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merch_id));
		$store['onlinetime']=json_decode($store['onlinetime'],true);
		if($store['mypeisong']){
			$store['out_pay']=$store['out_pay']?:$post['out_pay'];
		}else{
			$store['out_pay']=$post['out_pay'];
		}
		$store['out_pay']=price($store['out_pay']);
		//配送时间
		$days = array(
				0=>date("m-d",TIMESTAMP),
				1=>date("m-d",strtotime("+1 day")),
				2=>date("m-d",strtotime("+2 day")), 
				3=>date("m-d",strtotime("+3 day")), 
				4=>date("m-d",strtotime("+4 day")),
				5=>date("m-d",strtotime("+5 day")),
				6=>date("m-d",strtotime("+6 day")),
		);
		$totaytime = strtotime(date('Y-m-d'));
		//配送时间段
		$lasttimebox=0;
		if($post['set_timebox']==1){
			foreach($post['peisong_timebox'] as &$item){
				$start=$daytime.' '.$item['times']['start'];
				$start=strtotime($start);
				$end=$daytime.' '.$item['times']['end'];
				$end=strtotime($end);
				$times[]=array(
					'start' => $item['times']['start'],
					'end' => $item['times']['end'],
					'timestamp' => $end,
					'fee' => 0,
					'timev' =>$item['times']['start'].'~'.$item['times']['end'],
					'out_pay' => $store['out_pay']."元配送费",
				);
				$timestamp[] = $end;
				if($end>$item['yuyue']*60+time()){
					$lasttimebox +=1;
				}
			}
		}else{
			$timestamp = array();
			$daytime=date('Y-m-d');
			if($store['onlinetime']['start']){
				$start=$daytime.' '.$store['onlinetime']['start'];
				$start=strtotime($start);
			}else{
				$start = mktime(8, 0);
			}
			if($store['onlinetime']['end']){
				$end=$daytime.' '.$store['onlinetime']['end'];
				$end=strtotime($end);
			}else{
				$end = mktime(22, 0);
			}
			for($i = $start; $i < $end;) {
					$times[] = array(
						'start' => date('H:i', $i),
						'end' => date('H:i', $i + 1800),
						'timestamp' => $i + 1800,
						'fee' => 0,
						'timev' =>date('H:i', $i).'~'.date('H:i', $i + 1800),
						'out_pay' => $store['out_pay']."元配送费",
					);
					$timestamp[] = $i + 1800;
					$i += 1800;
					if($i + 1800>30*60+time()){
						$lasttimebox +=1;
					}
			}
		}
		$data = array(
			'days' => $days,
			'times' => $times,
			'timestamp' => $timestamp,
			'updatetime' => strtotime(date('Y-m-d')) + 86400,
			'reserve' => 0,
			'lasttimebox' => $lasttimebox,
		);
		return $data;
	}
	public function current_level_up_types(){
		return str_array($this->zhaoshang['level_up_types']);
	}
	public function upcomments($postdata=array(),$merchid=0){
		if(!$merchid){
			return false;
		}
		$comment_id=$this->pdo_getcolumn('comments',array('merchid'=>$merchid,'orderid'=>intval($postdata['orderid']),'goodsid'=>intval($postdata['goodsid'])),'id');
		$data=array(
			'content'=>$postdata['content'],
			'images'=>ijson_encode($postdata['images']),
			'star'=>intval($postdata['star']),
			'sendstar'=>intval($postdata['sendstar']),
			'fuwustar'=>intval($postdata['fuwustar']),
		);
		$allstar=$this->pdo_getsum('comments',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'star');
		$allstar +=intval($postdata['star']);
		$allstarcount=$this->pdo_count('comments',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
		$allstarcount +=1;
		$star=$allstar/$allstarcount;
		$this->pdo_update('user',array('star'=>$star),array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
		if($comment_id){
			$this->pdo_update('comments',$data,array('id'=>$comment_id));
		}else{
			$show=$this->setting['iscomment']?0:1;
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'show'=>$show,
				'merchid'=>$merchid,
				'goodsid'=>intval($postdata['goodsid']),
				'orderid'=>intval($postdata['orderid']),
				'createtime'=>TIMESTAMP,
			);
			$data=array_merge($insert,$data);
			$this->pdo_insert('comments',$data);
			$comment_id=pdo_insertid();
		}
		return $comment_id;
	}
	public function notice_shenhe($merch,$ispass=0,$wxapp_id=0){
		//申请结果通知
		$template_id=cfc('account')->get_tpl_template_id('OPENTM412123809','申请结果通知');
		$wxapp_id=$wxapp_id?:$_SESSION['wxapp_id'];
		if($wxapp_id){
			$wxapptemplate_id=cfc('wxapp_api',$wxapp_id)->get_tpl_template_id('10415',[5,4,2],'shenhe','审核结果通知','jy_merch');
		}
		$message=array(
			'first'=>array('value'=>'您好！您申请已处理完成。',"color" => "#4a5077"),
			'keyword1'=>array('value'=>'店铺《'.$merch['merchname'].'》的申请！',"color" => "#4a5077"),
			'keyword2'=>array('value'=>date('Y-m-d H:i:s',$merch['applytime']),"color" => "#4a5077"),
			'keyword5'=>array('value'=>$ispass?'审核通过':'审核失败',"color" => "#4a5077"),
			'remark'=>array('value'=>'感谢您的使用，点击查看详情！',"color" => "#4a5077"),
		);
		$wxappmsg=array(
			array('value'=>'店铺《'.$merch['merchname'].'》的申请！'),
			array('value'=>date('Y-m-d H:i:s',time())),
			array('value'=>$ispass?'审核通过':'审核失败'),
		);
		if(!$url){
			$url=custom_url('jy_merch/manage.index/',true);
		}
		cfc('account')->sendtwoNotice($merch['mid'],$template_id,$message,$url,'shenhe','jy_merch',$merch['id']);
		if($wxapp_id){
			$url='/jy_merch/manage/index/index';
			$template_id=cfc('wxapp_api',$wxapp_id)->checktplNotice($merch['mid'],$wxapptemplate_id,$wxappmsg,$url,'shenhe','jy_merch',$merch['id']);
		}
	}
	public function set_feelog($params,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$params['merchid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'msg'=>$params['msg'],
			'status'=>$params['status'],
			'gift_type'=>$params['gift_type'],
			'fee'=>$params['fee'],
			'type'=>$type,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_merch_feelog',$insert);
		return true;
	}
	//获取我的红包
	public function get_myred($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$this->model('member')->getred($mid);
		$red['list']=pdo_getall('jy_merch_feelog',array('type'=>0,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		foreach($red['list'] as &$sv){
			$sv['member']=cfc('core')->member_show($sv['mid']);
			$sv['createtime']=date('Y-m-d H:i:s',$sv['createtime']);
		}
		$red['member']=$_SESSION['member'];
		$red['allfee']=pdo_getsum('jy_merch_feelog',array('type'=>0,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fee');
		$red['isfee']=pdo_getsum('jy_merch_feelog',array('type'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fee');
		$red['lastfee']=pdo_getcolumn('jy_weishop_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'redfee');
		return $red;
	}
	//获取当前红包
	public function get_nowred($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		//红包事件
		if($this->setting['red_open'] && $this->setting['red_rate']){
			$sale_reds=pdo_get('jy_merch_feelog',array('createtime >='=>TIMESTAMP-10,'fee >'=>0,'type'=>0,'status'=>0,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		}
		return $sale_reds['fee'];
	}
	//获取当前红包
	public function red_fee($price=0){
		$mid=$mid?:$_SESSION['uid'];
		//红包事件
		if($this->setting['red_open'] && $this->setting['red_rate']){
			$fee=$price*$this->setting['red_rate']/100;
		}
		return $fee;
	}
	//商品支付后处理事件
	public function up_pay($order=array()){
		//红包事件
		if($this->setting['red_open']){
			$sets=pdo_getcolumn('jy_merch_user',array('id'=>$order['merchid'],'uniacid'=>$_SESSION['uniacid']),'sets');	
			$sets=json_decode($sets,true);
			if($sets['red_rate']){
				$red_rate=$sets['red_rate'];
			}else{
				$red_rate=$this->setting['red_rate'];
			}
			if(!$red_rate){
				return false;
			}
			$fee=$order['price']*$red_rate/100;
			$feelog=array(
				'mid'=>$order['mid'],
				'merchid'=>$order['merchid'],
				'msg'=>'购物返现金积分红包',
				'gift_type'=>0,
				'fee'=>$fee,
				'status'=>0,
			);
			$this->set_feelog($feelog);
			$this->model('member')->getred($order['mid']);
		}
		if($order['merchid']){
			$this->model('member')->up_addmember($order['merchid']);
		}
		return true;
	}
	//获取可设置的权限
	public function get_plugins(){
		$plugins=$this->model('plugins')->merch();
		$newplugins=array();
		foreach($plugins as $ki=>&$item){
			foreach($item['list'] as $k=>&$p){
				if($p['is_install']){
					$newplugins[$p['name']]=$p['title'];
				}
			}
		}
		return $newplugins;
	}
	//获取可设置的权限
	public function get_games(){
		$games=$this->model('games')->getall();
		$newgames=array();
		foreach($games as $k=>&$p){
			$isplugin=pdo_get('core_plugins',array('name'=>$p['name']),array('id','title','image','hide'));
			if($isplugin['id']){
				$newgames[$p['name']]=$p['title'];
			}
		}
		return $newgames;
	}
	//获取商户组设置
	public function get_groupset($merchid=0,$name='',$noexi=false){
		$groupid=pdo_getcolumn('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'groupid');
		if(!$groupid){
			return true;
		}
		$group=pdo_get('jy_merch_group',array('id'=>$groupid,'uniacid'=>$_SESSION['uniacid']),array('groupname','level','plugins','bankuai','menus','games'));	
		$group['bankuai']=$group['bankuai']?json_decode($group['bankuai'],true):array();
		$group['menus']=$group['menus']?json_decode($group['menus'],true):array();
		if($name){
			$plugins=$this->get_plugins();
			if($plugins[$name]){
				$check=strexists($group['plugins'],';'.$name.';');	
				if(!$check){
					if(!$noexi){
						exi('没有访问权限','error',$group['menus'][0]['action']);
					}else{
						return false;
					}
				}
			}
			return true;
		}
		return $group;
	}
	public function sendtypes($merchid,$sendtypes=array(),$default=false,$is_peisong=0){
		$merch=pdo_get('jy_merch_user',array('id'=>$merchid),array('id','sendtype','opensendtype','ismake'));
		if($merch['id'] && $merch['opensendtype']){
			$sendtypes=array();
			if(strpos($merch['opensendtype'],'1') !== false){
				$sendtype1=1;
				$sendtypes[1]='同城配送';
			}elseif($is_peisong){
				$is_peisong=0;
			}
			if(strpos($merch['opensendtype'],'2') !== false){
				$sendtypes[2]='到店自取';
			}
			if(strpos($merch['opensendtype'],'4') !== false){
				$sendtype1=4;
				if(!$sendtypes[1]){
					$sendtypes[1]='快递配送';
				}
			}
		}
		if(!$is_peisong && $sendtypes[1]){
			$sendtypes[1]='快递配送';
		}
		if($_SESSION['is_dish'] && $sendtypes[1]){
			$sendtypes[1]='外卖配送';
		}
		$merchset=$this->setting;
		if(!$merchset['sendtype']){
		}elseif($merchset['sendtype']==1){
			$sendtitle=$sendtypes[1]?:'快递配送';
			$sendtypes=array('1'=>$sendtitle);
		}elseif($merchset['sendtype']==2){
			$sendtitle=$sendtypes[2]?:'到店自取';
			$sendtypes=array('2'=>$sendtitle);
		}elseif($merchset['sendtype']==3){
			if($this->plugin_exists('jy_area_dian')){
				$jy_area_dian=plugin('jy_area_dian')->plugin_setting('page');
				$sendtitle=$jy_area_dian['agent_title']?$jy_area_dian['agent_title'].'自取':'自提点自取';
			}else{
				$sendtitle=$sendtypes[2]?:'到店自取';
			}
			$sendtypes=array('2'=>$sendtitle);
		}
		if($default){
			$default=$merch['sendtype']==4?1:$merch['sendtype'];
			$res=array('sendtypes'=>$sendtypes,'default'=>$default,'ismake'=>$merch['ismake'],'sendtype1'=>$sendtype1,'sendtype4'=>$sendtype4,'is_peisong'=>$is_peisong);
			if($jy_area_dian){
				$res['is_area_dian']=1;
				$res['area_diantitle']=$jy_area_dian['agent_title']?:'自提点';
			}
			return $res;
		}else{
			return $sendtypes;
		}
	}
	public function check_tihuo($merchid=0){
		$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
		$item=pdo_get('jy_merch_tihuo',$params);
		if($item['id']){
			return true;
		}else{
			return false;
		}
	}
	public function uptofee($order_id=0){
		$order=model('jy_weishop/order')->get_order($order_id);
		if(!$order['mid']){
			return false;
		}
		model('jy_commission/commission')->create($order);
		$set=json_decode($cashier['set'],true);
		//购物送积分
		$buy_credit_setting=plugin('jy_weishop')->plugin_setting('buy_credit');
		if($buy_credit_setting['open'] && $buy_credit_setting['time']=='pay'){
			$this->buy_credit($order);
		}
		$buy_credit3_setting=plugin('jy_weishop')->plugin_setting('buy_credit3');
		if($buy_credit3_setting['open'] && $buy_credit3_setting['time']=='pay'){
			$this->buy_credit3($order);
		}
		return true;
	}
	public function buy_credit($order){
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['merchid']));
		$buy_credit=plugin('jy_weishop')->plugin_setting('buy_credit');
		if($buy_credit['type']==1){
			//根据订单金额赠送积分
			if(!$buy_credit['options']){
				return true;
			}
			foreach($buy_credit['options'] as $item){
				if($order['price']>=$item['start'] && $order['price']<=$item['end']){
					$credit=$order['price']*$item['credit'];
					break;
				}
			}
		}elseif($buy_credit['type']==2){
			//根据商品赠送积分
			$credit=0;
			foreach($order['goods'] as $goods){
				$goods_credit=pdo_getcolumn('jy_weishop_goods_credit',array('goods_id'=>$goods['goodsid']),'credit');
				if($goods_credit){
					$credit+=$goods_credit*$goods['total'];
				}
			}
		}
		if($credit>0){
			if($this->setting['iskou_merch']){
					if($merch['mid']){
						$member=model('core/member')->detail($merch['mid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
						if($member['credit1']<$credit){
							exi('商户积分不足，消费送积分失败！','error');
						}
						model('core/member')->member_credit($merch['mid'],'credit1',$credit,false,'jy_merch','商户扣积分（送客户消费积分）');
					}else{
						exi('商户未绑定【店长】，消费送积分失败！','error');
					}
			}
			model('core/member')->add_credit1($order['mid'],$credit,$merch['merchname'].'消费赠送');
		}
		return true;
	}
	public function buy_credit3($order){
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['merchid']));
		$buy_credit=plugin('jy_weishop')->plugin_setting('buy_credit3');
		if($order['paytype']=='wechat' && strpos($buy_credit['paytypes'],';1;') === false){
			return false;
		}
		if($order['paytype']=='credit' && strpos($buy_credit['paytypes'],';2;') === false){
			return false;
		}
		if($order['paytype']=='alipay' && strpos($buy_credit['paytypes'],';3;') === false){
			return false;
		}
		if($buy_credit['type']==1){
			//根据订单金额赠送积分
			if(!$buy_credit['options']){
				return true;
			}
			foreach($buy_credit['options'] as $item){
				if($order['price']>=$item['start'] && $order['price']<=$item['end']){
					$credit=$order['price']*$item['credit'];
					if($this->setting['iskou_merch']){
							if($merch['mid']){
								$member=model('core/member')->detail($merch['mid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
								if($member['credit'.$item['type']]<$credit){
									exi('商户积分不足，消费送积分失败！','error');
								}
								$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']),array('username','nickname'));
								$nickname=$member['nickname']?:$member['username'];
								model('core/member')->member_credit($merch['mid'],'credit'.$item['type'],$credit,false,'jy_merch','商户扣积分（送['.$nickname.']消费积分）');
							}else{
								exi('商户未绑定【店长】，消费送积分失败！','error');
							}
					}
					model('core/member')->member_credit($order['mid'],'credit'.$item['type'],$credit,true,'jy_weishop',$merch['merchname'].'消费赠送');
					break;
				}
			}
		}elseif($buy_credit['type']==2){
			//根据商品赠送虚拟币
			$credit=0;
			foreach($order['goods'] as $goods){
				$goods_credit=pdo_get('jy_weishop_goods_credit',array('goods_id'=>$goods['goodsid']));
				if(substr($goods_credit['credit'],-1)=='%'){
					$goods_credit['credit']=($goods['price']*$goods['total']*substr($goods_credit['credit'],0,-1))/100;
				}
				//剔除积分
				if($goods_credit['credit'] && $goods_credit['type']>1){
					$credit=$goods_credit['credit']*$goods['total'];
					if($merch['mid']){
						$member=model('core/member')->detail($merch['mid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
						if($member['credit'.$goods_credit['type']]<$credit){
							exi('商户积分不足，消费送积分失败！','error');
						}
						$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']),array('username','nickname'));
						$nickname=$member['nickname']?:$member['username'];
						model('core/member')->member_credit($merch['mid'],'credit'.$goods_credit['type'],$credit,false,'jy_merch','商户扣积分（送['.$nickname.']消费积分）');
					}else{
						exi('商户未绑定【店长】，消费送积分失败！','error');
					}
					model('core/member')->member_credit($order['mid'],'credit'.$goods_credit['type'],$credit,true,'jy_weishop',$merch['merchname'].'消费赠送');
				}
			}
		}
		return true;
	}
    public function upmember($groupid=0,$mid=0){
        if(!$groupid || !$mid){
            return true;
        }
        $user=pdo_get('jy_weishop_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('id'));
        if(!$user['id']) {
            $insert = array(
                'mid' => $_SESSION['uid'],
                'uniacid' => $_SESSION['uniacid'],
                'createtime' => TIMESTAMP,
            );
            $this->register($insert);
        }
        $memberlevel_id=pdo_getcolumn('jy_merch_group',array('id'=>$groupid,'uniacid'=>$_SESSION['uniacid']),'memberlevel_id');
        pdo_update('jy_weishop_member',array('level'=>$memberlevel_id),array('merchid'=>0,'mid'=>$mid));
        $install = $this->plugin_exists('jy_commission');
        if($install){
            model('jy_commission/member')->register();
            $level_id=pdo_getcolumn('jy_commission_level',array('shoplevel_id'=>$memberlevel_id,'uniacid'=>$_SESSION['uniacid']),'id');
            pdo_update('jy_commission_member',array('level_id'=>$level_id),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
        }
        return true;
    }
	public function checkworker($type=0){
		if($_SESSION['workertype'] && $_SESSION['workertype']<$type){
			exi('您没有该权限！','error','jy_merch/manage.index/');
		}
	}
}